﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;

namespace SportsStore.Domain.Concrete
{
    public class EFProductRepository:IProductRepository
    {
        private KeLianEntities context = new KeLianEntities() ;

        public IQueryable<Product> Products
        {
            get { return context.Products; }
        }

        public void SaveProduct(Product product)
        {
            if (product.Id == 0)
                context.Products.AddObject(product);
            else
            {
                Product modifyingProduct = context.Products.FirstOrDefault(x => x.Id == product.Id);
                modifyingProduct.Name = product.Name;
                modifyingProduct.Description = product.Description;
                modifyingProduct.Price = product.Price;
                
                modifyingProduct.CategoryId = product.CategoryId;
                modifyingProduct.Ranking = product.Ranking;//should have a function do ranking specifically.
                product.ImageToShow = product.ImageToShow; //should do it separately
            }
            context.SaveChanges();
        }

        public void DeleteProduct(Product product)
        {
            context.Products.DeleteObject(product);
            context.SaveChanges();
        }

        
    }
}
